<template>
  <!-- <div></div> -->
  <div style="padding-top: 10px; margin-left: 10px">
    <div v-for="(item, index) in tableData" :key="index">
      <div style="display: flex">
        <div>
          <el-avatar :src="item.payment_logo" />
        </div>
        <div
          style="
            display: flex;
            justify-content: space-between;
            width: 80%;
            margin-left: 10px;
            margin-top: 1px;
          "
        >
          <div>
            <div>{{ item.payment_name }}</div>
            <div style="margin-top: 10px">{{ item.payment_explain }}</div>
          </div>
          <div
            v-permissions="{ permission: ['add:PaymentSetting'] }"
            style="display: flex"
          >
            <el-switch v-model="item.open_status" @change="changePay(item)" />
            <el-button
              v-if="item.open_status"
              style="height: 30px; margin-left: 10px"
              type="primary"
              @click="changePays(item)"
            >
              编辑
            </el-button>
          </div>
        </div>
      </div>
      <el-divider />
    </div>
    <el-dialog
      :before-close="handleClose"
      title="提示"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <div v-for="(item, index) in config" :key="index">
        <!-- item 值 {{ item }} --- index 键 {{ index }}--- i下标---{{ i }} -->
        <div>
          <el-form ref="form" label-width="100px">
            <el-form-item
              v-if="item == 'input' || item == 'textarea'"
              :label="langs.pay[index]"
            >
              <el-input v-model="configs[index]" />
            </el-form-item>
            <el-form-item v-if="item == 'file'" :label="langs.pay[index]">
              <el-upload
                action="http://fan.jb.cc/api/upload_cert"
                :before-remove="beforeRemove"
                class="upload-demo"
                :data="{ sub_id: configs['sub_mchid'] }"
                :headers="header"
                :limit="1"
                multiple
                :on-exceed="handleExceed"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :on-success="
                  (res) => {
                    return uploadSuccess(res, index)
                  }
                "
              >
                <el-button size="small" type="primary">点击上传</el-button>
              </el-upload>
            </el-form-item>
          </el-form>
        </div>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="sbutim">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import { sysPaymentList, sysPaymentAdd, sysPaymentDel } from '@/api/pay'
  // import { getUserList } from '@/api/public'

  export default {
    props: {},
    data() {
      return {
        tableData: [],
        langs: JSON.parse(window.localStorage.getItem('lang')),
        dialogVisible: false,
        config: {},
        form: {},
        configs: {},
      }
    },
    computed: {
      header() {
        return {
          Authorization: 'Bearer' + ' ' + localStorage.token,
        }
      },
    },
    mounted() {
      this.init()
    },
    beforeMount() {},
    methods: {
      init() {
        sysPaymentList().then((e) => {
          this.tableData = e.data
        })
      },
      changePay(e) {
        this.form.id = e.id
        this.form.payment_code = e.payment_code

        this.configs = JSON.parse(JSON.stringify(e.config))
        for (var c in this.configs) {
          this.configs[c] = ''
        }
        this.config = e.config
        if (e.open_status) {
          if (e.payment_data.length != 0) {
            const data = {
              id: e.id,
              is_del: 'N',
            }
            sysPaymentDel(data).then(() => {})
          } else {
            this.dialogVisible = true
          }
        } else {
          const data = {
            id: e.id,
            is_del: 'Y',
          }
          sysPaymentDel(data).then(() => {
            this.init()
          })
        }
      },

      changePays(e) {
        this.form.id = e.id
        this.form.payment_code = e.payment_code

        this.config = e.config
        if (e.open_status) {
          if (e.payment_data.length != 0) {
            this.configs = e.payment_data.payment_config
            console.log(this.configs)
            this.dialogVisible = true
          } else {
            this.dialogVisible = true
          }
        } else {
          const data = {
            id: e.id,
            is_del: 'Y',
          }
          sysPaymentDel(data).then(() => {
            this.init()
          })
        }
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(() => {
            done()
          })
          .catch(() => {})
      },
      //
      handleRemove(file, fileList) {
        console.log(file, fileList)
      },
      handlePreview(file) {
        console.log(file)
      },
      handleExceed(files, fileList) {
        this.$message.warning(
          `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
            files.length + fileList.length
          } 个文件`
        )
      },
      beforeRemove(file) {
        return this.$confirm(`确定移除 ${file.name}？`)
      },
      uploadSuccess(res, index) {
        this.configs[index] = res.data
      },
      sbutim() {
        this.form.payment_config = JSON.stringify(this.configs)
        sysPaymentAdd(this.form).then(() => {
          this.dialogVisible = false
          this.init()
        })
      },
      cancel() {
        this.tableData.forEach((e) => {
          if (e.id == this.form.id) {
            e.open_status = !e.open_status
            this.dialogVisible = false
          }
        })
      },
    },
  }
</script>

<style lang="scss" scoped></style>
